<?php
/**
 * Created by PhpStorm.
 * User: cephei
 * Date: 10/16/15
 * Time: 4:25 PM
 */

namespace App\Http\Practice\Lib;

use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class PracticeLib
{
    const GlobalStyle = array(
        'alignment' => array(
            'horizontal' => Alignment::HORIZONTAL_LEFT,
            'vertical' => Alignment::VERTICAL_CENTER,
            'wrapText' => false
        ),
        'font' => array(
            'size' => 15
        )
    );

    public static function getPractice1()
    {
        // 竖式
        $p1_1 = ArithmeticLib::getArithmeticList(1, false, false, [[11, 99], [11, 99]], [1, 99999], false, false, ['*']);
        $p1_2 = ArithmeticLib::getArithmeticList(1, false, false, [[100, 999], [3, 9]], [2, 999], false, false, ['/']);
        $p1_3 = ArithmeticLib::getArithmeticList(1, false, false, [[100, 999], [3, 9]], [2, 999], true, true, ['/']);

        // 递等式-混合
        $p2 = ArithmeticLib::getArithmeticList(3, true, true, [[10, 99], [2, 99], [2, 99]], [2, 99], false, false);
        // 递等式-加减
        $p2 = array_merge($p2, ArithmeticLib::getArithmeticList(1, true, true, [[100, 999], [100, 999], [100, 999]], [2, 999], false, false, ['+', '-'], ['+', '-']));
        shuffle($p2);

        // 口算-除法
        $p3 = ArithmeticLib::getArithmeticList(3, false, false, [[10, 99], [3, 9]], [2, 99], false, false, ['/']);
        // 口算-乘法
        $p3 = array_merge($p3, ArithmeticLib::getArithmeticList(3, false, false, [[11, 99], [2, 9]], [2, 99999], false, false, ['*']));
        // 口算-乘法2
        for ($i = 0; $i < 3; $i++) {
            $t1 = rand(11, 99);
            $t2 = rand(1, 9);
            $p3 = array_merge($p3, ArithmeticLib::getArithmeticList(1, false, false, [[$t1, $t1], [$t2 * 10, $t2 * 10]], [2, 99999], false, false, ['*']));
        }
        $p3 = array_merge($p3, RapidCalculation::getFormulas(3));

        shuffle($p3);


        $spreadSheet = new Spreadsheet();
        $sheet = $spreadSheet->getActiveSheet();

        // 全局样式
        $sheet->getDefaultRowDimension()->setRowHeight(20);
        $sheet->getDefaultColumnDimension()->setWidth(21);

        $sheet->setCellValueExplicitByColumnAndRow(1, 1, '1. 计算', DataType::TYPE_STRING);
        $sheet->getStyleByColumnAndRow(1, 1)->applyFromArray(self::GlobalStyle);
        for ($i = 0; $i < 3; $i++) {
            for ($j = 0; $j < 4; $j++) {
                $sheet->setCellValueExplicitByColumnAndRow($j + 1, $i + 1 + 1, $p3[$i * 4 + $j], DataType::TYPE_STRING);
                $sheet->getStyleByColumnAndRow($j + 1, $i + 1 + 1)->applyFromArray(self::GlobalStyle);
            }
        }

        $sheet->setCellValueExplicitByColumnAndRow(1, 6, '2. 递等式', DataType::TYPE_STRING);
        $sheet->getStyleByColumnAndRow(1, 6)->applyFromArray(self::GlobalStyle);

        $sheet->setCellValueExplicitByColumnAndRow(1, 7, $p2[0], DataType::TYPE_STRING);
        $sheet->setCellValueExplicitByColumnAndRow(3, 7, $p2[1], DataType::TYPE_STRING);
        $sheet->setCellValueExplicitByColumnAndRow(1, 9, $p2[2], DataType::TYPE_STRING);
        $sheet->setCellValueExplicitByColumnAndRow(3, 9, $p2[3], DataType::TYPE_STRING);
        $sheet->getStyleByColumnAndRow(1, 7)->applyFromArray(self::GlobalStyle);
        $sheet->getStyleByColumnAndRow(3, 7)->applyFromArray(self::GlobalStyle);
        $sheet->getStyleByColumnAndRow(1, 9)->applyFromArray(self::GlobalStyle);
        $sheet->getStyleByColumnAndRow(3, 9)->applyFromArray(self::GlobalStyle);
        $sheet->getRowDimension(8)->setRowHeight(150);
        $sheet->getRowDimension(10)->setRowHeight(150);

        $sheet->setCellValueExplicitByColumnAndRow(1, 11, '3. 竖式，第三题要求验算', DataType::TYPE_STRING);
        $sheet->getStyleByColumnAndRow(1, 11)->applyFromArray(self::GlobalStyle);

        $sheet->setCellValueExplicitByColumnAndRow(1, 12, $p1_1[0], DataType::TYPE_STRING);
        $sheet->setCellValueExplicitByColumnAndRow(2, 12, $p1_2[0], DataType::TYPE_STRING);
        $sheet->setCellValueExplicitByColumnAndRow(3, 12, $p1_3[0], DataType::TYPE_STRING);
        $sheet->getStyleByColumnAndRow(1, 12)->applyFromArray(self::GlobalStyle);
        $sheet->getStyleByColumnAndRow(2, 12)->applyFromArray(self::GlobalStyle);
        $sheet->getStyleByColumnAndRow(3, 12)->applyFromArray(self::GlobalStyle);

        return self::save($spreadSheet);
    }

    public static function getPractice2()
    {
        // 竖式
        $p1_1 = ArithmeticLib::getArithmeticList(1, false, false, [[11, 99], [11, 99]], [1, 99999], false, false, ['*']);
        $p1_2 = ArithmeticLib::getArithmeticList(1, false, false, [[100, 999], [3, 9]], [2, 999], false, false, ['/']);

        // 递等式-混合
        $p2 = ArithmeticLib::getArithmeticList(3, true, true, [[10, 99], [2, 99], [2, 99]], [2, 99], false, false);

        // 口算-除法
        $p3 = ArithmeticLib::getArithmeticList(5, false, false, [[10, 99], [3, 9]], [2, 99], false, false, ['/']);
        // 口算-乘法
        $p3 = array_merge($p3, ArithmeticLib::getArithmeticList(5, false, false, [[11, 99], [2, 9]], [2, 99999], false, false, ['*']));
        // 口算-乘法2
        for ($i = 0; $i < 5; $i++) {
            $t1 = rand(11, 99);
            $t2 = rand(1, 9);
            $p3 = array_merge($p3, ArithmeticLib::getArithmeticList(1, false, false, [[$t1, $t1], [$t2 * 10, $t2 * 10]], [2, 99999], false, false, ['*']));
        }
        $p3 = array_merge($p3, RapidCalculation::getFormulas(5));
        // 口算-加减法
        $p3 = array_merge($p3, ArithmeticLib::getArithmeticList(5, false, false, [[11, 99], [11, 99]], [2, 99999], false, false, ['+', '-'], ['+', '-']));
        shuffle($p3);
        $p3 = array_slice($p3, 0, 6);


        $spreadSheet = new Spreadsheet();
        $sheet = $spreadSheet->getActiveSheet();

        // 全局样式
        $sheet->getDefaultRowDimension()->setRowHeight(20);
        $sheet->getDefaultColumnDimension()->setWidth(30);

        $sheet->setCellValueExplicitByColumnAndRow(1, 1, '1. 计算', DataType::TYPE_STRING);
        $sheet->getStyleByColumnAndRow(1, 1)->applyFromArray(self::GlobalStyle);
        for ($i = 0; $i < 2; $i++) {
            for ($j = 0; $j < 3; $j++) {
                $sheet->setCellValueExplicitByColumnAndRow($j + 1, $i + 1 + 1, $p3[$i * 3 + $j], DataType::TYPE_STRING);
                $sheet->getStyleByColumnAndRow($j + 1, $i + 1 + 1)->applyFromArray(self::GlobalStyle);
            }
        }

        $sheet->setCellValueExplicitByColumnAndRow(1, 5, '2. 递等式', DataType::TYPE_STRING);
        $sheet->getStyleByColumnAndRow(1, 5)->applyFromArray(self::GlobalStyle);

        $sheet->setCellValueExplicitByColumnAndRow(1, 6, $p2[0], DataType::TYPE_STRING);
        $sheet->setCellValueExplicitByColumnAndRow(2, 6, $p2[1], DataType::TYPE_STRING);
        $sheet->setCellValueExplicitByColumnAndRow(3, 6, $p2[2], DataType::TYPE_STRING);
        $sheet->getStyleByColumnAndRow(1, 6)->applyFromArray(self::GlobalStyle);
        $sheet->getStyleByColumnAndRow(2, 6)->applyFromArray(self::GlobalStyle);
        $sheet->getStyleByColumnAndRow(3, 6)->applyFromArray(self::GlobalStyle);
        $sheet->getRowDimension(7)->setRowHeight(150);

        $sheet->setCellValueExplicitByColumnAndRow(1, 8, '3. 竖式', DataType::TYPE_STRING);
        $sheet->getStyleByColumnAndRow(1, 8)->applyFromArray(self::GlobalStyle);

        $sheet->setCellValueExplicitByColumnAndRow(1, 9, $p1_1[0], DataType::TYPE_STRING);
        $sheet->setCellValueExplicitByColumnAndRow(2, 9, $p1_2[0], DataType::TYPE_STRING);
        $sheet->getStyleByColumnAndRow(1, 9)->applyFromArray(self::GlobalStyle);
        $sheet->getStyleByColumnAndRow(2, 9)->applyFromArray(self::GlobalStyle);
        $sheet->getRowDimension(10)->setRowHeight(250);

        $sheet->setCellValueExplicitByColumnAndRow(1, 11, '草稿区域', DataType::TYPE_STRING);
        $sheet->getStyleByColumnAndRow(1, 11)->applyFromArray(self::GlobalStyle);
        $sheet->getStyleByColumnAndRow(1, 11)->getBorders()->getTop()->setBorderStyle(true);
        $sheet->getStyleByColumnAndRow(2, 11)->getBorders()->getTop()->setBorderStyle(true);
        $sheet->getStyleByColumnAndRow(3, 11)->getBorders()->getTop()->setBorderStyle(true);

        return self::save($spreadSheet);
    }

    private static function save($spreadSheet)
    {
        $writer = new Xlsx($spreadSheet);
        $fileName = sprintf('pratices/practice-%s.xlsx', date('YmdHis', time()));
        $writer->save(base_path(sprintf("public/%s", $fileName)));
        return $fileName;
    }
}